a=int(input())
b=0
delit=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89,97]
delit2=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
delit3=[2, 3, 5, 7]
fir=[]
if len(str(a))!=4:
    print(0)
else:
    for i in range(25):  #
        if a%delit[i]==0:
            break
        else:
            b+=1
    if b==25:
         print(a)#
    else:
        b=0
        for i2 in range(11):
            if (a//10) % delit2[i2] == 0:
                break
            else:
                b += 1
        if b == 11:
            fir.append(a//10)
        else:
            b=0
            for i3 in range(4):
                if (a//100) % delit3[i3] == 0:
                    break
                else:
                    b += 1
            if b == 4:
                fir.append(a // 100)
            else:
               b=0
               if (a//1000) in delit3:
                  fir.append(a//1000)
               else:
                   fir.append(0)

        b=0
        for j2 in range(11):
            if (a%1000) % delit2[j2] == 0:
                break
            else:
                b += 1
        if b == 11:
            fir.append(a%1000)
        else:
            b=0
            for j3 in range(4):
                if (a%100) % delit3[j3] == 0:
                    break
                else:
                    b += 1
            if b == 4:
                fir.append(a%100)
            else:
               if (a%10) in delit3:
                   fir.append(a%10)
               else:
                   fir.append(0)
        b=0
        for f in range(4):
            if ((a//10)%100) % delit3[f] == 0:
                break
            else:
                b += 1
        if b == 4:
            fir.append((a//10)%100)

        if (a//10)%10 in delit3:
            fir.append(a//10%10)
        if (a//100)%10 in delit3:
            fir.append((a//100)%10)

        print(max(fir))